home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / Apps / SoundApps / TimeWarp / Source / CompletionView.m < prev    next >
Encoding:
Text File  |  1995-06-12  |  2.7 KB  |  104 lines

  1. /* CompletionView.m
  2.  * CompletionView is a simple "gas gauge" display to display the proress
  3.  * of some calclation.  As you call [compltionView setDoubleValue:value]
  4.  * with values of 0.0 to 1.0, the completion view go from empty to full.
  5.  *
  6.  * You may freely copy, distribute, and reuse the code in this example.
  7.  * NeXT disclaims any warranty of any kind, expressed or  implied, as to its
  8.  * fitness for any particular use.
  9.  *
  10.  * Written by: Robert Poor
  11.  * Created: Sep/92
  12.  *
  13.  *  Below is the body of the .m file with detailed comments as appropriate.
  14.  */
  15.  
  16. #import "CompletionView.h"
  17. #import <appkit/Cell.h>
  18. #import <appkit/graphics.h>
  19. #import <dpsclient/psops.h>
  20.  
  21. @interface CompletionView(CompletionViewPrivate)
  22. - _setInstanceValue:(double *)instanceValue from:(double)newValue;
  23. @end
  24.  
  25. @implementation CompletionView:View
  26.  
  27. - initFrame:(const NXRect *)frameRect
  28. {
  29.   [super initFrame:frameRect];
  30.   backgroundColor = NX_LTGRAY;
  31.   completionColor = NX_DKGRAY;
  32.   updateTextField = NO;
  33.   /* set value and pctValue to out-of-range values ... */
  34.   value = -1.0;
  35.   pctValue = -1;
  36.   /* ... to force an update here */
  37.   [self setDoubleValue:0.0];
  38.   return self;
  39. }
  40.  
  41. - (double)value { return value; }
  42. - setDoubleValue:(double)newValue
  43. {
  44.   /* limit newValue to fall within 0 and 1 (inclusive) */ 
  45.   if (newValue < 0.0) newValue = 0.0;
  46.   else if (newValue > 1.0) newValue = 1.0;
  47.  
  48.   if (newValue != value) {
  49.     value = newValue;
  50.     [self display];
  51.     if (updateTextField) {
  52.       /* update the associated TextField, if any... */
  53.       int newPctValue = newValue * 100;
  54.       if (newPctValue != pctValue) {
  55.     char buf[5];
  56.     pctValue = newPctValue;
  57.     sprintf(buf,"%3d%%",pctValue);
  58.     [textField setStringValue:buf];
  59.       }
  60.     }
  61.   }
  62.   return self;
  63. }
  64.  
  65. - (float)backgroundColor { return backgroundColor; }
  66. - setBackgroundColor:(float)color { backgroundColor = color; return self; }
  67. - (float)completionColor { return completionColor; }
  68. - setCompletionColor:(float)color { completionColor = color; return self; }
  69.  
  70. - textField { return textField; }
  71. - setTextField:aTextField
  72. {
  73.   if (aTextField && [aTextField respondsTo:@selector(setStringValue:)]) {
  74.     textField = aTextField;
  75.     updateTextField = YES;
  76.     return self;
  77.   } else {
  78.     return nil;
  79.   }
  80. }
  81.  
  82. - drawSelf:(NXRect *)rects :(int)rectCount
  83. {
  84.   NXRect completionRect = bounds;
  85.  
  86.   /* erase the background */
  87.   PSsetgray (backgroundColor);
  88.   NXRectFill(&bounds);
  89.  
  90.   if (bounds.size.width > bounds.size.height) {
  91.     /* grow horizontally */
  92.     completionRect.size.width = bounds.size.width * value;
  93.   } else {
  94.     /* grow vertically */
  95.     completionRect.size.height = bounds.size.height * value;
  96.   }
  97.   PSsetgray (NX_DKGRAY);
  98.   NXRectFill(&completionRect);
  99.  
  100.   return self;
  101. }
  102.  
  103. @end
  104.